package com.ruoyi.web.controller.business;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.Statement;
import com.ruoyi.business.service.IStatementService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 结算单Controller
 * 
 * @author ruoyi
 * @date 2022-01-25
 */
@Controller
@RequestMapping("/business/statement")
public class StatementController extends BaseController {
    private String prefix = "business/statement";

    @Autowired
    private IStatementService statementService;

    @RequiresPermissions("business:statement:view")
    @GetMapping("/listPage")
    public String listPage() {
        return prefix + "/list";
    }

    /**
     * 查询结算单列表
     */
    @RequiresPermissions("business:statement:list")
    @PostMapping("/query")
    @ResponseBody
    public TableDataInfo query(Statement statement) {
        startPage();
        List<Statement> list = statementService.selectStatementList(statement);
        return getDataTable(list);
    }

    /**
     * 导出结算单列表
     */
    @RequiresPermissions("business:statement:export")
    @Log(title = "结算单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Statement statement) {
        List<Statement> list = statementService.selectStatementList(statement);
        ExcelUtil<Statement> util = new ExcelUtil<Statement>(Statement.class);
        return util.exportExcel(list, "结算单数据");
    }

    /**
     * 新增结算单
     */
    @GetMapping("/addPage")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存结算单
     */
    @RequiresPermissions("business:statement:add")
    @Log(title = "结算单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Statement statement) {
        int rows = statementService.addSave(statement);
        return toAjax(rows);
    }

    /**
     * 修改结算单
     */
    @RequiresPermissions("business:statement:edit")
    @GetMapping("/editPage/{id}")
    public String edit(@PathVariable("id") Long id, Model model) {
        Statement statement = statementService.selectStatementById(id);
        model.addAttribute("statement", statement);
        return prefix + "/edit";
    }

    /**
     * 修改保存结算单
     */
    @RequiresPermissions("business:statement:edit")
    @Log(title = "结算单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Statement statement) {
        int rows = statementService.editSave(statement);
        return toAjax(rows);
    }

    /**
     * 删除结算单
     */
    @RequiresPermissions("business:statement:remove")
    @Log(title = "结算单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(Long ids) {
        return toAjax(statementService.deleteStatementById(ids));
    }


}
